package com.zhengdongzulin.zuke.app.main.apartment

import androidx.lifecycle.rxLifeScope
import com.google.gson.Gson
import com.lxj.androidktx.core.sp
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.app.data.HouseTypeDetaisl
import com.zhengdongzulin.zuke.app.data.UserInfo
import com.zhengdongzulin.zuke.base.BaseViewModel
import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toResponse

/**
 * @类名称: CLASS
 * @类描述:
 * @创建人：Jeff
 * @创建时间：2021/6/21 22:35
 * @备注：
 */
class HouseDetailsVm : BaseViewModel() {
    var room_type_detail: StateLiveData<HouseTypeDetaisl> = StateLiveData()

    fun room_type_detail(id: String?) {
        rxLifeScope.launch(
            {
                var userInfo: UserInfo? = null
                var user = sp().getString("userInfo", "")
                if (!user.isNullOrEmpty()) {
                    userInfo = Gson().fromJson<UserInfo>(user, UserInfo::class.java)
                }
                var r = RxHttp.postJson(Api.ROOM_TYPE_DETAIL)
                    .add("id", id)
                    .add("userId", userInfo?.userId)
                    .toResponse<HouseTypeDetaisl>()
                    .await()
                room_type_detail.postValueAndSuccess(r)
            }, {
                assignment(it, room_type_detail)
            }
        )
    }

    var collection_add: StateLiveData<String> = StateLiveData()

    fun collection_add(id: String?) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.COLLECTION_ADD)
                    .add("roomTypeId", id)
                    .toResponse<String>()
                    .await()
                collection_add.postValueAndSuccess(r)
            }, {
                assignment(it, collection_add)
            }
        )
    }

    var collection_cancel: StateLiveData<String> = StateLiveData()

    fun collection_cancel(id: String?) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.COLLECTION_CANCEL)
                    .add("roomTypeId", id)
                    .toResponse<String>()
                    .await()
                collection_cancel.postValueAndSuccess(r)
            }, {
                assignment(it, collection_cancel)
            }
        )
    }


    var check_sign_online: StateLiveData<String> = StateLiveData()

    fun check_sign_online(id: String?) {
        rxLifeScope.launch(
            {
                var r = RxHttp.postJson(Api.CHECK_SIGN_ONLINE)
                    .add("roomTypeId", id)
                    .toResponse<String>()
                    .await()
                check_sign_online.postValueAndSuccess(r)
            }, {
                assignment(it, check_sign_online)
            }
        )
    }
}